<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '轮播图添加' : '轮播图编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- 原生图片上传 -->
        <el-form-item label="图片">
          <div class="imgFile">
            <h2>+</h2>
            <input type="file" @change="changeImg1" v-if="obj.isTrue" />
            <img :src="imgUrl" v-if="imgUrl" />
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd">
          添 加
        </el-button>
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { successAlert, warningAlert } from "../../../util/alert";
import { bannerAdd, bannerInfo, bannerEdit } from "../../../request/api";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      // 渲染图片
      imgUrl: "",
      form: {
        title: "",
        img: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      bannerList: "banner/list",
    }),
  },
  methods: {
    ...mapActions({
      reqBannerList: "banner/reqChangeList",
    }),
    // 原生图片上传
    changeImg1(e) {
      // e.target.files[0] 图片所有信息
      let file = e.target.files[0];
      // 图片大小单位默认为B,  1024B=1KB    1024KB=1MG  1024MB=1GB
      if (file.size >= 2 * 1024 * 1024) {
        warningAlert("图片大小超出限制，请重新选择");
        return;
      }
      // URL.createObjectURL()    可以通过图片信息，将信息转换成具体的图片
      this.imgUrl = URL.createObjectURL(file);
      // 将file传给后端
      this.form.img = file;
    },
    // 封装清空
    empty() {
      this.form = {
        title: "",
        img: "",
        status: 1,
      };
      this.imgUrl = "";
    },
    // 点击取消
    cancel() {
      this.obj.isTrue = false;
      this.empty();
    },
    // 点击添加
    submit() {
      bannerAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqBannerList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条信息
    getOne(id) {
      bannerInfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.form.id = id;
        this.imgUrl = this.$pre + this.form.img;
      });
    },
    // 点击了编辑
    edit() {
      bannerEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqBannerList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {},
};
</script>
<style scoped>
/* 1、原生图片上传 */
.imgFile {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  position: relative;
}
.imgFile h2 {
  line-height: 200px;
  font-size: 50px;
  text-align: center;
  margin: 0 auto;
}
.imgFile input {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 3;
}
.imgFile img {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
}
</style>